# @Version : 1.0
# @Author  : wxz
# @File    : user_dao.py
# @Time    : 2025/6/1 21:13
# @Describe: 描述...
from fastapi import Depends
from sqlalchemy.exc import MultipleResultsFound, NoResultFound
from sqlmodel import Session, select

from apps.user.data_model.user_ext_info_model import UserExtInfo
from apps.user.data_model.user_model import User
from commons.common_exception import SystemException
from config.app_logger import logger
from database.mysql.mysql_client import MySQLClient


class UserDAO:

    def __init__(self, db_session: Session = Depends(MySQLClient.get_db_session),):
        self.db_session = db_session

    def find_by_id(self, user_id):
        return self.db_session.get(User, user_id)

    def find_user_by_username(self, username: str):
        """
        根据用户名查询
        :param username:
        :return:
        """
        try:
            stmt = (select(User)
                    .where(User.username == username)
                    .where(User.status == 0)
                    .where(User.is_delete == 0)
                    )
            user = self.db_session.exec(stmt).one()

            return user

        except MultipleResultsFound as e1:
            logger.error(e1, exc_info=True)
            return None
        except NoResultFound as e2:
            logger.error(e2, exc_info=True)
            return None
        except Exception as e3:
            logger.error(e3, exc_info=True)
            raise SystemException


    def save_user(self, user: User)-> bool:
        """
        保存用户
        :param user_ext_info:
        :param user:
        :return:
        """
        self.db_session.add(user)
        return True







